﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Configuration;
using System.Diagnostics;

/*
* University of Reykjavík
* Brjóstagjöf.is is the product of first-year practical programming course
* for computer science students at said university
*
* URL: http://code.google.com/p/scags 
* Authors: 
* Ástvaldur Sigurðsson
* Guðjón Jónsson
* Kristín Inga Vigfúsdóttir
* Steinar Bragi Sigurðsson
* Sveinn Ernstsson
*/

namespace skags.Models
{
    public class LogErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            if (filterContext.Exception != null)
            {
                /*    var log = new Logger();
                    log.LogException(filterContext.Exception); */
            }
        }
    }

    public class Logger
    {
        private List<LogMedia> m_loggers = new List<LogMedia>();

        public Logger()
        {
            m_loggers.Add(new OutputWindowLogMedia());
            m_loggers.Add(new EmailLogMedia());
        }

        public void LogException(Exception ex)
        {
            foreach (var obj in m_loggers)
            {
                string templateMessage =
                    "Date: " + DateTime.Now +
                    "\n\nMessage: " + ex.Message +
                    "\n\nStackTrace: " + ex.StackTrace +
                    "\n\nSource: " + ex.Source +
                    "\n\n----------------------------------\n\n\n";

                obj.LogMessage(templateMessage);
            }

            // Loggum einnig í gagnagrunn
            var _dc = new DBDataContext();
            _dc.ErrorLoggings.InsertOnSubmit(new ErrorLogging
                            {
                                DateCreated = DateTime.Now,
                                ErrorMessage = ex.Message,
                                StackTrace = ex.StackTrace,
                                ErrorSource = ex.Source,
                                ErrorType = ex.GetType().ToString(),
                                TargetSite = ex.TargetSite.ToString()
                            });
            _dc.SubmitChanges();
        }
    }

    public class LogMedia
    {
        public virtual void LogMessage(string message) { }
    }

    // Output window
    public class OutputWindowLogMedia : LogMedia
    {
        public override void LogMessage(string message)
        {
            Debug.WriteLine(message);
        }
    }

    // Send to email
    public class EmailLogMedia : LogMedia
    {
        DBDataContext _dc = new DBDataContext();

        public override void LogMessage(string message)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(ConfigurationManager.AppSettings["EmailAddress"]);
            mail.Subject = "Brjostagjof.is: Error logging";
            mail.Body = message;

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(mail);
        }

        public void SendSubscriptionUpdate()
        {
            var Emails = from p in _dc.vw_PostLists select p;

            List<MailMessage> mail = new List<MailMessage>();

            foreach (var item in Emails)
            {
                MailMessage message = new MailMessage();
                message.To.Add(item.Email);
                message.Subject = "Ný Frétt/Grein";
                message.Body = "kíktu á http://www.brjostagjof.is";

                mail.Add(message);
            }

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;

            foreach (var mailMessage in mail)
            {
                client.Send(mailMessage);
            }

            /* mig vantar samt bara að fá s.subscription.equals(1) og samsvarandi e-mail. */
        }

        public void ReportAbuse(ForumThread thread, UserInfo user, DateTime timestamp)
        {
           

            MailMessage mail = new MailMessage();
            mail.To.Add(ConfigurationManager.AppSettings["EmailAddress"]);
            mail.Subject = "Brjostagjof.is: Óviðeigandi þráður";
            mail.Body = "   Höfundur Þráðar: " + thread.aspnet_User.UserName.ToString() + " - " + user.UserID + System.Environment.NewLine +
                        "   Þráður: Forum/Thread/" + thread.ID + System.Environment.NewLine +
                        "   Hver kvartar: " + user.FullName + " - " + user.UserID + System.Environment.NewLine +
                        "   Tími: " + timestamp;

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(mail);
        }

        public void ReportAbuse(ThreadAnswer answer, UserInfo user, DateTime timestamp)
        {

            MailMessage mail = new MailMessage();
            mail.To.Add(ConfigurationManager.AppSettings["EmailAddress"]);
            mail.Subject = "Brjostagjof.is: Óviðeigandi svar";
            mail.Body = "   Höfundur Svars: " + answer.aspnet_User.UserName.ToString() + " - " + user.UserID + System.Environment.NewLine +
                        "   Þráður: Forum/Thread/" + answer.ThreadID + System.Environment.NewLine +
                        "   Númer svars: " + answer.ID + System.Environment.NewLine +
                        "   Hver kvartar: " + user.FullName + " - " + user.UserID + System.Environment.NewLine +
                        "   Tími: " + timestamp;

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(mail);
        }

        public void ApplyForProMail(string message, string user)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(ConfigurationManager.AppSettings["EmailAddress"]);
            mail.Subject = "Umsókn um breyttan aðgang";
            mail.Body = "Eftirfarandi notandi: " + user.ToString() +
                        " hefur óskað eftir að fá breytt aðgengi að síðunni." + System.Environment.NewLine
                        + "Notandi hafði eftirfarandi að segja: " + System.Environment.NewLine + message;

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(mail);
        }        
    }

}